package com.zjw.zy.componse.unityui.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.zjw.zy.componse.unityui.R;
import com.zjw.zy.componse.unityui.service.DemoSocketServiceViewModel;
import com.zjw.zy.componse.unityui.unity.BaseUnityContentView;

/**
 * 请如此替换
 * <p>
 * #code{
 * UnityPlayActivity extends Activity{
 * ...
 * onCreate(Bundle saveInstance){
 * ...
 * mUnityPlayer = new UnityPlayer(this, this);
 * DemoUnityViewContent demoUnityViewContent = new DemoUnityViewContent(this);
 * demoUnityViewContent.attachUnityView(mUnityPlayer, UnityPlayer::UnitySendMessage);
 * setContentView(demoUnityViewContent);
 * ...
 * }
 * ...
 * }
 * }
 */
public class DemoUnityViewContent extends BaseUnityContentView {

    private final DemoSocketServiceViewModel viewModel = new DemoSocketServiceViewModel();

    public DemoUnityViewContent(@NonNull Context context) {
        super(context);
    }

    public DemoUnityViewContent(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public DemoUnityViewContent(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public DemoUnityViewContent(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onBindListener() {

        viewModel.connectService();

        viewModel.getReplyLiveData().observeForever(s -> {
            sendUnityMessage("MainPlayer", "AndroidControlMove", s);
        });

        unityOperatorContainer.findViewById(R.id.demo_skip_op).setOnTouchListener((v, event) -> {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                sendUnityMessage("MainPlayer", "AndroidControlFire", "");
                return true;
            }
            return true;
        });

        MoveView moveView = unityOperatorContainer.findViewById(R.id.demo_move_op);
        moveView.bindListener(new MoveView.Listener() {
            @Override
            public void onMoveStart() {

            }

            @Override
            public void onMove(float horizontal, float vertical) {
                viewModel.sendServiceMessage(vertical + "," + horizontal + ",");
                //sendUnityMessage("MainPlayer", "AndroidControlMove", vertical + "," + horizontal);
            }

            @Override
            public void onMoveEnd() {

            }
        });
    }

    @Override
    protected View onOperatorContentView() {
        return LayoutInflater.from(getContext()).inflate(R.layout.unity_operator_layout, this, false);
    }
}
